package com.boot.mall.seckill.controller;

import com.alibaba.fastjson.JSON;
import com.boot.common.utils.R;
import com.boot.mall.seckill.service.SecKillService;
import com.boot.mall.seckill.vo.SecKillSkuRedisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SecKillController {

    @Autowired
    private SecKillService secKillService;

    /**
     * 查询当前时间段的可以参与秒杀的场次
     * @return
     */
    @GetMapping("/getCurrentSeckillSkus")
    public R getCurrentSecKillSessionSku(){
        List<SecKillSkuRedisVo> redisVos = secKillService.getCurrentSecKillSessionSku();
        return R.ok().put("data",redisVos);
    }

    /**
     * 远程调用接口，用以查询sku是否在秒杀时间段内
     * @param skuId
     * @return
     */
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSecKillInfo(@PathVariable("skuId") Long skuId){
        SecKillSkuRedisVo vo = secKillService.getSkuSecKillInfo(skuId);
        String json = JSON.toJSONString(vo);
        return R.ok().put("data",json);
    }

    /**
     * 所有秒杀的接口使用seckill开始
     * 秒杀请求
     */
    public R kill(String killId,String key,Integer num){
        // 执行秒杀业务，秒杀成功返回一个订单号，秒杀失败返回null
        String orderSn = secKillService.kill(killId,key,num);
        return R.ok();
    }

}
